'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const { INTEGER, STRING, BOOLEAN, DATE, TEXT } = Sequelize;
    
    await queryInterface.createTable('sys_menus', {
      id: {
        type: Sequelize.BIGINT,
        primaryKey: true,
        comment: '菜单ID',
      },
      parent_id: {
        type: Sequelize.BIGINT,
        allowNull: true,
        defaultValue: 0,
        comment: '父菜单ID',
      },
      name: {
        type: STRING(50),
        allowNull: false,
        comment: '菜单名称',
      },
      path: {
        type: STRING(255),
        allowNull: true,
        comment: '路由路径',
      },
      component: {
        type: STRING(255),
        allowNull: true,
        comment: '组件路径',
      },
      icon: {
        type: STRING(100),
        allowNull: true,
        comment: '图标',
      },
      type: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 1,
        comment: '菜单类型：1目录，2菜单，3按钮',
      },
      permission: {
        type: STRING(100),
        allowNull: true,
        comment: '权限标识',
      },
      sort: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '排序',
      },
      visible: {
        type: BOOLEAN,
        defaultValue: true,
        comment: '是否显示：true显示，false隐藏',
      },
      status: {
        type: BOOLEAN,
        defaultValue: true,
        comment: '状态：true启用，false禁用',
      },
      create_time: {
        type: DATE,
        allowNull: false,
        comment: '创建时间',
      },
      update_time: {
        type: DATE,
        allowNull: false,
        comment: '更新时间',
      },
    });
    
    // 添加索引
    await queryInterface.addIndex('sys_menus', ['parent_id']);
    await queryInterface.addIndex('sys_menus', ['type']);
    await queryInterface.addIndex('sys_menus', ['status']);
    await queryInterface.addIndex('sys_menus', ['visible']);
    await queryInterface.addIndex('sys_menus', ['sort']);
  },

  down: async (queryInterface, Sequelize) => {
    // 先删除索引
    await queryInterface.removeIndex('sys_menus', ['parent_id']);
    await queryInterface.removeIndex('sys_menus', ['type']);
    await queryInterface.removeIndex('sys_menus', ['status']);
    await queryInterface.removeIndex('sys_menus', ['visible']);
    await queryInterface.removeIndex('sys_menus', ['sort']);
    // 再删除表
    await queryInterface.dropTable('sys_menus');
  },
};